import { defineStore } from "pinia";
import type { reqCategoryType } from "@/api/attr";
import {
  requestCategory1,
  requestCategory2,
  requestCategory3,
} from "@/api/attr";

//state的类型限制
interface storeType {
    categoryList1: reqCategoryType;
    categoryList2: reqCategoryType;
    categoryList3: reqCategoryType;
    category1Id: number | null;
    category2Id: number | null;
    category3Id: number | null;
}
const useAttrStore = defineStore("attr",{
    state:():storeType=>{
        return {
            categoryList1:[],
            categoryList2:[],
            categoryList3:[],
            category1Id:null,
            category2Id:null,
            category3Id:null,
        }
    },
    getters:{

    },
    actions:{
        //请求一级数据
        async getCategory1(){
            try {
               const result =  await requestCategory1()
               this.categoryList1 = result
            } catch (error) {
                error
            }
        },
        //请求二级数据
        async getCategory2(){
            try {

                const result =  await requestCategory2(this.category1Id as number)
                this.categoryList2 = result
             } catch (error) {
                error
             }
        },
        //请求三级数据
        async getCategory3(){
            try {
                if(!this.category2Id) return
                const result =  await requestCategory3(this.category2Id as number)
                this.categoryList3 = result
             } catch (error) {
                error
             }
        }
    }
})

export default useAttrStore